#*******************************************************************************
#                                                                              *
#     Author:      G. Leber                                                    *
#                  Technische Universitaet Wien                                *
#                  Institut fuer Technische Informatik E182/1                  *
#                  Treitlstrasse 3                                             *
#                  1040 Wien                                                   *
#                  Tel.: (0222) 58801 / 8176                                   *
#                  E-Mail: guenther@vmars.tuwien.ac.at                         *
#     File:        Makefile                                                    *
#     Version:     1.9                                                         *
#     Date:        6/27/94                                                     *
#                                                                              *
#******************************************************************************/

LIB     =       /usr/local/lib
INCLUDE =       /usr/local/include
MAN3    =       /usr/local/man/man3

OBJS    =       sem182.o msem182.o

CC      =       gcc
AR	=       ar
RANLIB  =       ranlib

#GCC_VERSION := $(shell $(CC) -v 2>&1 | $(AWK) '/^gcc version /{gsub("\\.", " ", $$3); print $$3}')
GCC_VERSION := $(strip $(shell $(CC) -dumpversion))

# standard flags for gcc: We want a certain version, at least -O2/-Os since some warnings are only triggered there
# and position-independent code for a shared object
CFLAGS   =      -std=gnu89 \
		-Os \
		-fPIC

# add warnings for catching stupid and stylistic errors
CFLAGS  +=      -Wall \
                -Wformat-security \
                -Wswitch-enum \
                -Wundef \
                -Wshadow \
                -Wbad-function-cast \
                -Wcast-align \
                -Wwrite-strings \
                -Wsign-compare \
                -Waggregate-return \
                -Wstrict-prototypes \
                -Wmissing-prototypes \
                -Wmissing-declarations \
                -Wmissing-noreturn \
                -Wmissing-format-attribute \
	        -Winline \
	        -Wdisabled-optimization \
	        -Wcast-qual

# gcc-3.3.5 on Debian sarge doesn't know the following warnings
ifneq ("$(GCC_VERSION)","3.3.5")
CFLAGS  +=      -Wextra \
                -Wold-style-definition \
                -Wdeclaration-after-statement
endif
# gcc-4 has more warnings
ifeq ("$(firstword $(subst ., , $(GCC_VERSION)))","4")
# gcc-3.4.3 on Centos-4.1 reports "will never be executed" even if there is nothing to detect
CFLAGS  +=      -Wmissing-field-initializers \
                -Werror
else
CFLAGS  +=      -Wunreachable-code
endif

INSTALL =       install

DOXYGEN =       doxygen
EXCLUDE_PATTERN=footrulewidth
CD      =       cd >/dev/null
MV      =       mv
GREP    =       grep

.PHONY: all clean uninstall doc pdf


all: libsem182.a libsem182.so

libsem182.a: $(OBJS)
	$(AR) cru libsem182.a $?
	$(RANLIB) libsem182.a

libsem182.so: $(OBJS)
	$(CC) -shared -o $@ $^

clean:
	$(RM) $(OBJS) libsem182.a libsem182.so *~
	$(RM) -r doc

install: $(MAN3) $(INCLUDE)/sem182.h  $(INCLUDE)/msem182.h \
	$(LIB)/libsem182.a $(LIB)/libsem182.so \
	$(MAN3)/sem182.3 $(MAN3)/msem182.3

$(INCLUDE)/sem182.h: sem182.h
	$(INSTALL) -c -m 644 sem182.h $(INCLUDE)

$(INCLUDE)/msem182.h: msem182.h
	$(INSTALL) -c -m 644 msem182.h $(INCLUDE)

$(LIB)/libsem182.a: libsem182.a
	$(INSTALL) -c -m 644 libsem182.a $(LIB)

$(LIB)/libsem182.so: libsem182.so
	$(INSTALL) -c -m 644 libsem182.so $(LIB)

$(MAN3):
	$(INSTALL) -d $(MAN3)

$(MAN3)/sem182.3: sem182.3
	$(INSTALL) -c -m 644 sem182.3 $(MAN3)
	$(RM) -f $(MAN3)/P.3
	$(RM) -f $(MAN3)/V.3
	$(RM) -f $(MAN3)/seminit.3
	$(RM) -f $(MAN3)/semgrab.3
	$(RM) -f $(MAN3)/semrm.3
	ln $(MAN3)/sem182.3 $(MAN3)/P.3
	ln $(MAN3)/sem182.3 $(MAN3)/V.3
	ln $(MAN3)/sem182.3 $(MAN3)/seminit.3
	ln $(MAN3)/sem182.3 $(MAN3)/semgrab.3
	ln $(MAN3)/sem182.3 $(MAN3)/semrm.3

$(MAN3)/msem182.3: msem182.3
	$(INSTALL) -c -m 644 msem182.3 $(MAN3)
	$(RM) -f $(MAN3)/mP.3
	$(RM) -f $(MAN3)/mV.3
	$(RM) -f $(MAN3)/mseminit.3
	$(RM) -f $(MAN3)/msemgrab.3
	$(RM) -f $(MAN3)/msemrm.3
	ln $(MAN3)/msem182.3 $(MAN3)/mP.3
	ln $(MAN3)/msem182.3 $(MAN3)/mV.3
	ln $(MAN3)/msem182.3 $(MAN3)/mseminit.3
	ln $(MAN3)/msem182.3 $(MAN3)/msemgrab.3
	ln $(MAN3)/msem182.3 $(MAN3)/msemrm.3

uninstall:
	$(RM) $(INCLUDE)/sem182.h  $(INCLUDE)/msem182.h $(LIB)/libsem182.a \
	        $(MAN3)/sem182.3 $(MAN3)/P.3 $(MAN3)/V.3 $(MAN3)/seminit.3 \
	        $(MAN3)/semgrab.3 $(MAN3)/semrm.3 $(MAN3)/msem182.3 \
	        $(MAN3)/mP.3 $(MAN3)/mV.3 $(MAN3)/mseminit.3 \
	        $(MAN3)/msemgrab.3 $(MAN3)/msemrm.3

doc:
	$(DOXYGEN) doxygen.dcf

pdf: doc
	$(CD) doc/pdf && \
	$(MV) refman.tex refman_save.tex && \
	$(GREP) -v $(EXCLUDE_PATTERN) refman_save.tex > refman.tex && \
	$(RM) refman_save.tex && \
	make && \
	$(MV) refman.pdf refman.save && \
	$(RM) *.pdf *.html *.tex *.aux *.sty *.log *.eps *.out *.ind *.idx \
	      *.ilg *.toc *.tps Makefile && \
	$(MV) refman.save refman.pdf
